package cn.com.dstz.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils{

	public static int strToTime( String string, String format ) {
		if( string == null || string.length( ) == 0 ){
			return 0;
		}

		SimpleDateFormat sdf = getSimpleDateFormat( format );
		try{
			Date ndate = sdf.parse( string );
			return ( int ) ( ndate.getTime( ) / 1000 );
		}catch( Exception e ){
			e.printStackTrace( );
			return 0;
		}
	}

	public static String formatDate( Date date, String format ) {
		SimpleDateFormat dataFormat = getSimpleDateFormat( format );

		if( date == null ){
			date = new Date( );
		}
		return dataFormat.format( date );
	}

	/**
	 * 
	 * @param format
	 * @param timeoffset
	 * @return
	 */
	public static SimpleDateFormat getSimpleDateFormat( String format ) {
		SimpleDateFormat sdf = new SimpleDateFormat( format );
		return sdf;
	}

	public static String getCurrentDate( ) {
		return getCurrentTime( "yyyy-MM-dd" );
	}

	public static String getCurrentTime( String format ) {
		return formatDate( null, format );
	}

	public static String getCurrentTime( ) {
		return getCurrentTime( "yyyy-MM-dd HH:mm:ss" );
	}
	
	public static String formatDateByAddYear( Date d, int year ){
		Calendar c = Calendar.getInstance( );
		c.setTime( d );
		c.add( Calendar.YEAR, year );
		
		Date newDate = c.getTime( );
		
		return formatDate( newDate, "yyyy-MM-dd");
	}
	
	public static void main( String [ ] args ) {
		
		Calendar c = Calendar.getInstance( );
		Date date = new Date( );
		c.setTime( date );
		
		c.add( Calendar.YEAR, 1 );
		
		System.out.println( c.getTime( ) );
		
		
	}
}
